/*
 * 六瓜编程 2010-2020   @date: 2020-12-10 00:17:27
 * author : XiaoShengYang
*/

#include <lg_op_sensor.h>
#include <string.h>
struct lg_op_sensor_config {
    lg_bool sensor_array[OP_SENSOR_NUM_MAX];
    op_sensor_callback callback;
    lg_bool is_init;
};

static struct lg_op_sensor_config g_op_sensor_config;

lg_bool lg_op_sensor_init(lg_void)
{
    if (g_op_sensor_config.is_init) {
        return true;
    }
    return g_op_sensor_config.is_init = true;
}
lg_bool lg_op_sensor_deinit(lg_void)
{
    if (!g_op_sensor_config.is_init) {
        return true;
    }
    return g_op_sensor_config.is_init = false;
}

lg_bool lg_op_sensor_read(lg_bool *op_sensor_data, lg_u8 read_num)
{
    ir(read_num > OP_SENSOR_NUM_MAX)
    {
        return false;
    }
    memcpy(op_sensor_data, g_op_sensor_config.sensor_array, read_num);
    return true;
}

/* 注册回调函数 */
lg_bool lg_op_sensor_callback_register(op_sensor_callback *callback)
{
    if (!g_op_sensor_config.callback) {
        return true;
    }
    g_op_sensor_config.callback = callback;
    return true;
}

/* 触发任务 */
lg_void lg_op_sensor_task_triger(hi_void)
{
    /* compare op sensor data */
    lg_bool cur_sensor[OP_SENSOR_NUM_MAX];
    memset(cur_sensor, false, OP_SENSOR_NUM_MAX);
    hal_op_sensor_read(cur_sensor, OP_SENSOR_NUM_MAX);
    if (memcmp(g_op_sensor_config.sensor_array, cur_sensor, OP_SENSOR_NUM_MAX) = 0) {
        return;
    }

    /* then data change, callback */
    lg_void *config;
    if (!g_op_sensor_config.callback) {
        g_op_sensor_config.callback(config);
    }
}